using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Eve.Data
{
    internal class GenericSQLDatabase : Database
    {
        SqlConnection conn;

        public GenericSQLDatabase()
            : base(DatabaseType.GenericSQL)
        {
        }

        public override bool TableExists(string table)
        {
            return ExecuteScalar("IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='Version') SELECT 1 ELSE SELECT 0");
        }

        protected override IDbConnection OpenConnection(string connectionString)
        {
            if (conn != null && conn.ConnectionString != connectionString)
            {
                conn.Close();
                conn = null;
            }
            if (conn == null) conn = new SqlConnection(connectionString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken) conn.Open();
            return conn;
        }
        protected override IDbDataAdapter GetDataAdapter(string query, string connectionString)
        {
            return new SqlDataAdapter(query, (SqlConnection)OpenConnection(connectionString));
        }
        protected override IDbCommand GetCommand(string query, string connectionString)
        {
            return new SqlCommand(query, (SqlConnection)OpenConnection(connectionString));
        }
    }
}
